/*
编写 : Hexh
时间 : 2012-5-25 15:23:01
*/
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core.Entities;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
	/// <summary>
	///	 the repository of InfraApproveLog
	/// </summary>
	public class InfraApproveLogRepositoryImpl : RepositoryNhbImpl<InfraApproveLog>, IInfraApproveLogRepository
	{

		/// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <returns>True， False</returns>
		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}

		// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <param name="where">Where 条件</param>
		/// <returns>True， False</returns>
		public bool IsFieldExistWhere(string fieldName, string fieldValue, string id, string where)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, where);
		}

		/// <summary>
		/// 查询 InfraApproveLog
		/// </summary>
		/// <param name="query">查询条件</param>
		/// <param name="start">分页开始</param>
		/// <param name="limit">分页大小</param>
		/// <param name="sort">排序条件</param>
		/// <param name="dir">排序方向</param>
		/// <param name="filters">筛选条件</param>
		/// <param name="total">返回记录条数</param>
		/// <returns>返回记录集</returns>
		public IList<InfraApproveLog> GetPlist(string approveItemId, string approveType, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");

			//  修改查询条件ID1 ID2
			string strQuery = (string.IsNullOrEmpty(strFilter) ? " 1=1 " : strFilter)
				+ (string.IsNullOrEmpty(approveItemId) ? string.Empty : " and ( t.ApproveItemId = :approveItemId ) ")
				+ (string.IsNullOrEmpty(approveType) ? string.Empty : " and (t.InfraAppr.ApproveType = :approveType ) ");

			// sort
			string strSort = string.IsNullOrEmpty(sort) ? string.Empty : " order by t." + sort + " " + dir;

			//  修改关联查询条件 OuterColumn
			string strSql = @" from InfraApproveLog as t 
									inner join fetch t.User
									left join fetch t.ApproveUser"
				+ (string.IsNullOrEmpty(strQuery) ? string.Empty : " where " + strQuery);

			// 查询
			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql.Replace(" join fetch ", " join "));

			// 设置查询条件
			if (!string.IsNullOrEmpty(approveItemId))
			{
				selQuery.SetString("approveItemId", approveItemId);
				totQuery.SetString("approveItemId", approveItemId);
			}
			if (!string.IsNullOrEmpty(approveType))
			{
				selQuery.SetString("approveType", approveType);
				totQuery.SetString("approveType", approveType);
			}

			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<InfraApproveLog>();
		}

		/// <summary>
		/// 根据审批单返回所有审批步骤
		/// </summary>
		/// <param name="itemId"></param>
		/// <returns></returns>
		public IList<InfraApproveLog> GetAllByItemId(string itemId)
		{
			string strQuery = @"select t from InfraApproveLog as t where t.ApproveItemId = :itemId order by t.ApproveStep asc";
			var selQuery = Session.CreateQuery(strQuery);
			selQuery.SetString("itemId", itemId);
			return selQuery.List<InfraApproveLog>();
		}

		/// <summary>
		/// 删除已有的审批信息
		/// </summary>
		/// <param name="applItemId"></param>
		/// <param name="applId"></param>
		/// <returns></returns>
		public bool DeleteApplLogHist(string applItemId, string applId)
		{
			string strSql = @"delete from InfraApproveLog where ApproveItemId = :applItemId and ApproveId = :applId";
			var delQuery = Session.CreateQuery(strSql);
			delQuery.SetString("applItemId", applItemId).SetString("applId", applId);
			return delQuery.ExecuteUpdate() == 0;
		}
	}
}
